<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<html>
  <head>
    
    <title>readnote hello</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
  </head>
  
  <body>

	<div>
		<h1>获取所有model数据集合</h1>
		书籍:<select name="query.query['eq_book.id_l']" id="selectBook"  ></select>
		
		<input type="button" value="ajax all  " onclick="in1();">
		<input type="button" value="ajax book " onclick="in2();">
		
		<input type="button" value="ajax all：默认选择36 " onclick="in3();">
	
	</div>
	
	<div>
		<h1>获取数据字典数据集合</h1>
		是否状态:<select name="query.query['eq_book.id_l']" id="selectIsOpenStatus"  ></select>
		
		<input type="button" value="ajax all data diction(数据字典) " onclick="in4();">
		<input type="button" value="ajax all data diction(数据字典)：默认选择否 " onclick="in5();">
	</div>
	
	
	<div>
		<h1>获取数据集合，二级联动，根据父级ID</h1>
		用户：<select name="query.query['eq_userBook.user.id_l']" id="selectUser"  ></select>
		用户书籍:<select name="query.query['eq_userBook.id_l']" id="selectUserBook"  ></select>
		
		
		<p>
			<input type="button" value="ajax all data initLink(初始化数据，并绑定联动事件) " onclick="in6();">
			<br>
			<input type="button" value="ajax all data initLink(初始化数据，并绑定联动事件)：存在默认值 " onclick="in6_1();">
		</p>
	</div>
	
	
	<div>
		<h1>获取数据集合，多级联动，根据父级ID</h1>
		用户：<select name="query.query['eq_userBook.user.id_l']" id="selectUser_link"  ></select>
		用户书籍:<select name="query.query['eq_userBook.id_l']" id="selectUserBook_link"  ></select>
		用户笔记:<select name="query.query['eq_userNote.id_l']" id="selectUserNote_link"  ></select>
		
		<div id="userNoteContent"></div>
		<p>
			<input type="button" value="ajax all data initLink(初始化数据，并绑定联动事件) " onclick="in7();">
			<br>
			<input type="button" value="ajax all data initLink(初始化数据，并绑定联动事件)：存在默认值 " onclick="in8();">
		</p>
	</div>
	
	<div id="hiddenLinks">
	</div>
	

	<!-- 前台通用Jquery引用 -->
	<script type="text/javascript" src="<%=path%>/js/jquery/jquery-1.4.2.js"></script>
	<script type="text/javascript">
		//html document 加载完毕，执行初始化
		$("document").ready(function (){
			//JqajaxSelect.initSelectAll("selectBook","/ajax/all/ajaxBooks.do");
			//JqajaxSelect.initSelectAll("selectBook","/ajax/book/ajaxBooks.do");
		});
		
		// ajax 通用
		function in1(){
			JqajaxSelect.initSelectAll("selectBook","/ajax/all/ajaxBooks.do");
			
		}
		
		// ajax 书籍
		function in2(){
			JqajaxSelect.initSelectAll("selectBook","/ajax/book/ajaxBooks.do");
		}
		
		// ajax 通用，且存在默认值
		function in3(){
			JqajaxSelect.initSelectAll("selectBook","/ajax/all/ajaxBooks.do",36);
		}
		
		// ajax 通用，数据字典
		function in4(){
			JqajaxSelect.initSelectAll("selectIsOpenStatus","/ajax/all/ajaxDataDictYesNo.do");
		}
		
		// ajax 通用，数据字典，且存在默认值
		function in5(){
			JqajaxSelect.initSelectAll("selectIsOpenStatus","/ajax/all/ajaxDataDictYesNo.do",0);
		}
		
		// ajax 通用，获取数据集合，二级联动
		function in6(){
			// 初始化
			JqajaxSelect.initSelectAll("selectUser","/ajax/all/ajaxUsers.do");
			JqajaxSelect.bindLinkSelect("selectUser","selectUserBook","/ajax/all/ajaxUserBooks.do","selectUser",1);
			//bindLinkSelect:function (selectId,sonSelectId,ajaxSonDataUrl,linkName,linkOrder,currentSonOptionValue)
		}
		// ajax 通用，获取数据集合，二级联动，且存在默认值
		function in6_1(){
			// 初始化
			JqajaxSelect.initSelectAll("selectUser","/ajax/all/ajaxUsers.do",7);
			JqajaxSelect.bindLinkSelect("selectUser","selectUserBook","/ajax/all/ajaxUserBooks.do","selectUser",1,26);
			//bindLinkSelect:function (selectId,sonSelectId,ajaxSonDataUrl,linkName,linkOrder,currentSonOptionValue)
		}
		
		// ajax 通用，获取数据集合，多级联动，根据父级ID
		function in7(){
			// 初始化
			JqajaxSelect.initSelectAll("selectUser_link","/ajax/all/ajaxUsers.do");
			//initSelectAll:function (selectId,ajaxUrl,currentOptionValue)
			JqajaxSelect.bindLinkSelect("selectUser_link","selectUserBook_link","/ajax/all/ajaxUserBooks.do","selectUser_link",1);
			JqajaxSelect.bindLinkSelect("selectUserBook_link","selectUserNote_link","/ajax/all/ajaxUserNotes.do","selectUser_link",2);
			
			
			//bindLinkSelect:function (selectId,sonSelectId,ajaxSonDataUrl,linkName,linkOrder,currentSonOptionValue)
			in9();
		}
		
		// ajax 通用，获取数据集合，多级联动，根据父级ID，且存在默认值
		function in8(){
			// 初始化
			JqajaxSelect.initSelectAll("selectUser_link","/ajax/all/ajaxUsers.do",1);
			//initSelectAll:function (selectId,ajaxUrl,currentOptionValue)
			JqajaxSelect.bindLinkSelect("selectUser_link","selectUserBook_link","/ajax/all/ajaxUserBooks.do","selectUser_link",1,31);
			JqajaxSelect.bindLinkSelect("selectUserBook_link","selectUserNote_link","/ajax/all/ajaxUserNotes.do","selectUser_link",2,95);
			//bindLinkSelect:function (selectId,sonSelectId,ajaxSonDataUrl,linkName,linkOrder,currentSonOptionValue)
			
			in9();
		}
		
		//function in9
		function in9(){
			$("selectUserNote_link").change(function (){
				var url = "/ajax/all/ajaxUserNotesContent.do";
	               var params = {
                       parentId:$("selectUserNote_link").val()
	               };
	               JqajaxSelect.debug("getContent url:"+url);
	               jQuery.post(url, params, callbackFun, 'json');
				
				function callbackFun(data){
					var c = data.usernote.content;
					$("userNoteContent").html(c);
				}
				//<div id="userNoteContent"></div>
			});
		}
	</script>
	<script type="text/javascript">
		/**
		* 下拉列表jquery ajax 数据获取对象
		**/
		var JqajaxSelect={
			// 初始化，获取所有数据集合
			//	--	selectId	当前下拉框ID
			//	--	url			获取下拉框数据 ajax请求地址
			//	--	linkName	缺省。。当前属于父级别名:根级别，默认为selectId
			//	--	linkOrder	缺省。。当前属于父级联动子级的级别：根级别：值默认为0，当前为父级
			//	--	currentOptionValue	当前下拉框option默认值
			initSelectAll:function (selectId,ajaxUrl,currentOptionValue){
				JqajaxSelect.debug("--------------------------------------------------");
				JqajaxSelect.debug("linkOrder:root");
				JqajaxSelect.debug("initSelectAll:"+selectId);
				
				var _selectId = "#"+selectId;
				var _currentOptionValue = currentOptionValue;
				var _ajaxUrl = ajaxUrl;
				
				var linkName=selectId;	//默认根别名： select
				var linkOrder=0;		//默认根级别: 0
				
				
				// 1 初始化隐藏标签，封装为initLinkOrder
				//initLinkOrder:function (sonSelectId,linkName,linkOrder,currtOptionValue){
				JqajaxSelect.initLinkOrder(selectId,linkName,linkOrder,currentOptionValue);
				
				
				
				// selectId option数据获取
				//$("#selectBook").ready(function(){
				$(_selectId).ready(function(){
					//var url = '/ajax/book/ajaxBooks.do';
					var url = _ajaxUrl;
	                var params = {
	                        //name:$('#name').attr('value')
	                };
	                JqajaxSelect.debug(url);
	                jQuery.post(url, params, callbackFun, 'json');
				});
				
				//回执函数
				function callbackFun(data){
					JqajaxSelect.debug("callbackFun...");
					JqajaxSelect.debug("options："+data.options ); 
					//JqajaxSelect.debug("list："+data.list ); 
					//JqajaxSelect.debug("books："+data.books); 
					//JqajaxSelect.debug(data.innerHTML ); 
					//JqajaxSelect.debug(data.options ); 
			
	            	//JqajaxSelect.debug("回执："+data);
					//1、回执：
					//清空列表
					$(_selectId).empty();
					
					// 清空子级
	         		JqajaxSelect.removeSonSelect(linkName,linkOrder);
											
	            	//2、构成下拉框
	            	//循环结果
	            	$(data.options).each(function (index,item){
	            		//JqajaxSelect.debug(item.text);
	            		if(item.value==_currentOptionValue){
	            			//JqajaxSelect.debug(item.value);
    						$(_selectId).append("<option selected value="+item.value+">"+item.text+"</option>");
	            		}else{
		            		$(_selectId).append("<option value="+item.value+">"+item.text+"</option>");
	            		}
	            	});
	            	
	         		JqajaxSelect.debug("options end...： selected value "+$(_selectId).val());
	         		
	         		
	         		
	         		
	                //document.writeln(data);//对应HelloWorld类的message属性
	                //获取数据后渲染页面
	                
					//-- end get options
	            }
	            
				//-- end initSelectAll
			},
			
			//绑定联动事件，当前select 由 父级 select 选项变化时，获取数据
			//	--	selectId		当前下拉框ID
			//	--	sonSelectId		当前子级下拉框ID
			//	--	ajaxSonDataUrl	获取子级下拉框数据 ajax请求地址
			//	--	linkName		当前属于父级联动子级的级别
			//	--	linkOrder		当前属于父级联动子级的级别：从1开始。例如：1\2\3\4分别代表根下的第一级、二级、三级、四级联动下拉框
			//	--	currentSonOptionValue	当前子级下拉框option默认值
			bindLinkSelect:function (selectId,sonSelectId,ajaxSonDataUrl,linkName,linkOrder,currentSonOptionValue){
				JqajaxSelect.debug("--------------------------------------------------");
				JqajaxSelect.debug("linkOrder:"+linkOrder);
				JqajaxSelect.debug("bindLinkSelect");
				
				var _selectId = "#"+selectId;
				var _sonSelectId = "#"+sonSelectId;
				var _linkName=linkName;
				var _linkOrder=linkOrder;
				var _currentSonOptionValue = currentSonOptionValue;
				var _ajaxSonDataUrl = ajaxSonDataUrl;
				
				// 1 初始化
				/**
				var _linkSonHiddenName=_linkName;
				for(var i=0;i<_linkOrder;i++ ){
					_linkSonHiddenName=_linkSonHiddenName+"_";
				}
				JqajaxSelect.debug(_linkSonHiddenName);
				var _linkSonHidden=$("#"+_linkSonHiddenName);
				
				if(_linkSonHidden.length>0){
					JqajaxSelect.debug(_linkSonHiddenName+" exits:"+_linkSonHidden.val());
				}else{
					$("#hiddenLinks").append("<input type='hidden' name='"+_linkSonHiddenName+"' id='"+_linkSonHiddenName+"' value='"+_sonSelectId+"' />");
					JqajaxSelect.debug($("#hiddenLinks").html());
				}
				**/
				
				// 1 初始化，封装为initLinkOrder
				//initLinkOrder:function (sonSelectId,linkName,linkOrder,currtOptionValue){
				JqajaxSelect.initLinkOrder(sonSelectId,linkName,linkOrder,currentSonOptionValue);
				
				// 2 selectId 联动事件绑定
				//$("#selectUser").ready(function(){
				
				// 2-A、实现方式，内嵌回调
				/*
				$(_selectId).change(function(){
					//2.0 清空子级
	                //var son = $("#"+_linkSonHiddenName).val();
	                //JqajaxSelect.debug("清空子级ID:"+son);
	                //son.empty();
	                //JqajaxSelect.removeSonSelect(_linkSonHiddenName);
					
					//2.1 获取数据 
					//var url = '/ajax/book/ajaxBooks.do';
					var url = _ajaxSonDataUrl;
	                var params = {
	                        parentId:$(_selectId).val()
	                };
	                JqajaxSelect.debug("bind onchange:"+url);
	                JqajaxSelect.debug("parentId:"+$(_selectId).val());
	                
	                jQuery.post(url, params, callbackFun, 'json');
				});

				*/
				
				// 2-B、实现方式，声明式，引用回调				
				$(_selectId).change(changeSonsInit);
				function changeSonsInit(){
					
					//2.0 清空子级
	                //var son = $("#"+_linkSonHiddenName).val();
	                //JqajaxSelect.debug("清空子级ID:"+son);
	                //son.empty();
	                //JqajaxSelect.removeSonSelect(_linkSonHiddenName);
	                JqajaxSelect.removeSonSelect(linkName,linkOrder);
					
					//2.1 获取数据 
					//var url = '/ajax/book/ajaxBooks.do';
					var url = _ajaxSonDataUrl;
					var _parentId=$(_selectId).val();
					JqajaxSelect.debug("当前下拉框 数据加载前 parentId:"+_parentId);
					if( ( _parentId == null || typeof(_parentId) == "undefined" ) &&
						( _currentSonOptionValue != null && _currentSonOptionValue != "" || typeof(_currentSonOptionValue) != "undefined") ){
						// 当前下拉框无数据，根据当前级别上级下拉框默认值进行查询
						var _linkSonHiddenName= JqajaxSelect.getLinkSonHiddenName(linkName,linkOrder-1);
						JqajaxSelect.debug("当前下拉框无数据，根据当前级别上级下拉框默认值进行查询 parentId:"+_parentId);
						JqajaxSelect.debug("当前下拉框无数据，根据当前级别上级下拉框默认值进行查询 parentId:"+_linkSonHiddenName);
						var _son = $("#"+_linkSonHiddenName)
						if(_son.length>0){
							// 子级存在，执行清空
							var _val =  _son.val();
							var _values =  _val.split(",");
							var _sonId = _values[0];
							var _value = _values[1];
							
							
							_parentId = _value; 
							JqajaxSelect.debug("当前下拉框无数据，根据上一级隐藏标签获取 parentId:"+_parentId);
							
						}
					}
					
	                var params = {
	                        parentId:_parentId
	                };
	                JqajaxSelect.debug("bind onchange:"+url);
	                JqajaxSelect.debug("parentId:"+$(_selectId).val());
	                
	                jQuery.post(url, params, callbackFun, 'json');
	                
	                
				}
				
				// 2 ajax请求完毕，回执函数
				function callbackFun(data){
					JqajaxSelect.debug("callbackFun...");
					JqajaxSelect.debug("options："+data.options ); 
					//JqajaxSelect.debug(data.innerHTML ); 
					//JqajaxSelect.debug(data.options ); 
			
	            	//JqajaxSelect.debug("回执："+data);
					//1、回执：
					//清空列表
					$(_sonSelectId).empty();
											
	            	//2、构成下拉框
	            	//循环结果
	            	$(data.options).each(function (index,item){
	            		//JqajaxSelect.debug(item.text);
	            		if(item.value==_currentSonOptionValue){
	            			//JqajaxSelect.debug(item.value);
    						$(_sonSelectId).append("<option selected value="+item.value+">"+item.text+"</option>");
	            		}else{
		            		$(_sonSelectId).append("<option value="+item.value+">"+item.text+"</option>");
	            		}
	            	});
	            	
	            	JqajaxSelect.debug("options end...： selected value "+$(_sonSelectId).val()); 
	         
	                //document.writeln(data);//对应HelloWorld类的message属性
	                //获取数据后渲染页面
	            }
	            
	            
	            
	            var waitCurrt=function (){
	            	var tmpId = $(_selectId).val();
	            	JqajaxSelect.debug("wait..."+tmpId+$(_selectId).html());
		            	
	            	if( tmpId <= 0 ){
						// 当前级别数据ajax加载延迟，未获取到，延迟等待
						JqajaxSelect.debug("wait... value is null : "+tmpId);
						//setInterval(waitCurrt,1000);
						//setTimeout(waitCurrt,10000);
						
						JqajaxSelect.debug("wait... sleep 500 ms ");
						//JqajaxSelect.sleep(500);
						//setTimeout(function (){},500);
						waitCurrt();
					}else{
						JqajaxSelect.debug("wait... value is not null : "+tmpId);
					}
					return ;
	            }
	            
	            JqajaxSelect.debug("_currentSonOptionValue is not null ; select change()："+_currentSonOptionValue);
	            //3 触发子级数据获取，当子级存在默认值
				if(_currentSonOptionValue > 0 ){
					JqajaxSelect.debug("当前子级默认值："+_currentSonOptionValue);
					//waitCurrt();
					
					JqajaxSelect.debug("wait end....");
					
					$(_selectId).change();
				}
				
				
				
				//-- end bindLinkSelect
			},
			removeSonSelect: function(linkName,linkOrder){
				JqajaxSelect.debug("removeSonSelect linkName :"+linkName);
				JqajaxSelect.debug("removeSonSelect linkOrder :"+linkOrder);
				var _sonSelectName=linkName;
				var _linkSonHiddenName= JqajaxSelect.getLinkSonHiddenName(linkName,linkOrder);
				JqajaxSelect.debug("hiddenLinks:"+_linkSonHiddenName);
				var _son = $("#"+_linkSonHiddenName)
				if(_son.length>0){
					// 子级存在，执行清空
					var _val =  _son.val();
					var _values =  _val.split(",");
					var _sonId = _values[0];
					var _value = _values[1];
					
					JqajaxSelect.debug("removeSonSelect empty sonId:"+_values[0]);
					JqajaxSelect.debug("removeSonSelect empty sonId value:"+_values[1]);
					$(_sonId ).empty();
					
					linkOrder = linkOrder + 1 ;
					JqajaxSelect.removeSonSelect(linkName,linkOrder);
				}
				//-- end removeSonSelect
			},
			
			// 初始化联动隐藏标签
			initLinkOrder:function (currentSelectId,linkName,linkOrder,currtOptionValue){
				
				// 当前下拉框ID（根下子级） 下拉框ID
				var _sonSelectId = "#"+currentSelectId;
				// 当前级别根ID（根别名）
				var _linkName=linkName;
				// 当前所属根，子级级别：0 为根；1，为根下第一级联动；2，为二级联动；3，为三级联动；以此类推
				var _linkOrder=linkOrder;
				// 当前级别默认选中值
				var _currtOptionValue=currtOptionValue;
				if (typeof(_currtOptionValue) == "undefined") {
				   _currtOptionValue=-1;
				}   
				
				
				// 1 初始化根级、子级ID
				// user
				// user_
				// user__
				
				/**
				var _linkSonHiddenName=_linkName+"_hidden";
				for(var i=0;i<_linkOrder;i++ ){
					_linkSonHiddenName=_linkSonHiddenName+"_";
				}
				**/
				var _linkSonHiddenName= JqajaxSelect.getLinkSonHiddenName(linkName,linkOrder);
				JqajaxSelect.debug("hiddenLinks:"+_linkSonHiddenName);
				
				// 根据ID判定是否已经存在，并进行初始化赋值
				// input type=hidden name=user	value=select
				// input type=hidden name=user_
				var _linkSonHidden=$("#"+_linkSonHiddenName);
				if(_linkSonHidden.length>0){
					JqajaxSelect.debug("hiddenLinks::"+_linkSonHiddenName+" exits:"+_linkSonHidden.val());
				}else{
					$("#hiddenLinks").append("<input type='hidden' name='"+_linkSonHiddenName+"' id='"+_linkSonHiddenName+"' value='"+_sonSelectId+","+_currtOptionValue+"' />");
					JqajaxSelect.debug("hiddenLinks:"+$("#hiddenLinks").html());
				}
				//-- end initLinkOrderHidden
			},
			
			// 获取当前级别隐藏标签别名
			getLinkSonHiddenName:function (linkName,linkOrder){
				var _linkSonHiddenName=linkName+"_hidden";
				for(var i=0;i<linkOrder;i++ ){
					_linkSonHiddenName=_linkSonHiddenName+"_";
				}
				return _linkSonHiddenName;
				//-- end getLinkSonHiddenName
			},
			
			//-- end JqajaxSelect
			debug:function (message){
				//init debugMessage
				var _dm = $("#debugMessage");
				if(_dm.length<=0){
					$("body").append("<div id='debugMessage'></div>");
				}
				//
				if(message != ""){
					//$("body").append(message);	
					//$("body").append("<br>");
					
					// 元素内，追加 append
					/**
					var now =new Date();
					_dm.append(now.toLocaleString());
					_dm.append(":");		
					_dm.append(message);
					_dm.append("<br>");
					**/
					
					// 元素外，前置追加before
					//var now =new Date();
					//_dm.before(now.toLocaleString()+" : "+message+"<br>");
					
					//元素内，前置并追加 prepend
					var now =new Date();
					_dm.prepend(now.toLocaleString()+" : "+message+"<br>");
					
				}
				//end debug
			},
			
			// 输入毫秒，进入休眠...
			sleep:function (milisecond){
				var currentDate,beginDate=new Date();
				var beginHour,beginMinute,beginSecond,beginMs;
				var hourGaps,minuteGaps,secondGaps,msGaps,gaps;
				beginHour=beginDate.getHours();
				beginMinute=beginDate.getMinutes();
				beginSecond=beginDate.getSeconds();
				beginMs=beginDate.getMilliseconds();
				
				do {
				   currentDate=new Date();
				   hourGaps=currentDate.getHours() - beginHour;
				   minuteGaps=currentDate.getMinutes() - beginMinute;
				   secondGaps=currentDate.getSeconds() - beginSecond;
				   msGaps=currentDate.getMilliseconds() - beginMs;     
				   if(hourGaps<0)   hourGaps+=24;   //考虑进时进分进秒的特殊情况
				   gaps=hourGaps*3600+ minuteGaps*60+ secondGaps;
				   gaps=gaps*1000+msGaps;
				   
				   JqajaxSelect.debug("休眠剩余时间:"+gaps);
				}while(gaps<milisecond);  
				//end sleep
			}
		}
		
		
		
	</script>
	
	
	
  </body>
</html>
